package com.zjitc.controller;

import com.github.pagehelper.PageInfo;
import com.zjitc.pojo.Country;
import com.zjitc.service.CountryService;
import java.util.List;
import javax.xml.ws.RequestWrapper;

import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/26 15:13
 * @description:
 */
@SuppressWarnings("ALL")
@Controller
public class CountryController {

  @Autowired
  private CountryService service;

  @RequestMapping("/findAll/{start}/{count}")
  public ModelAndView findAll(
      @PathVariable("start")Integer start,
      @PathVariable("count")Integer count){
    List<Country> countries = service.findCountries(start,count);
    PageInfo info = new PageInfo(countries);
    ModelAndView mv = new ModelAndView();
    System.out.println(info);
    mv.addObject("page",info);
    mv.setViewName("/WEB-INF/view/pageData.jsp");
    return mv;
  }

  /**
   * 模糊查询
   * @param region
   * @return
   */
  @RequestMapping("/searchCountry")
  public ModelAndView searchCountry(@RequestParam(value = "region") String region) {
    System.out.println(region.length());
    ModelAndView mv = new ModelAndView();
    if(region.length() != 0) {
      List<Country> country = service.searchCountry(region);
      PageInfo info = new PageInfo(country);
      mv.addObject("region",region);
      mv.addObject("page", info);
      mv.setViewName("/WEB-INF/view/pagefind.jsp");
    }else{
      mv.setViewName("/findAll/0/10.action");
    }
    return mv;
  }

  @RequestMapping("/searchfind/{start}/{count}/{region}")
  public ModelAndView searchfind(
          @PathVariable("start")Integer start,
          @PathVariable("count")Integer count,
          @PathVariable("region") String region) {
    System.out.println(region.length());
    ModelAndView mv = new ModelAndView();
    if(region.length() != 0) {
      List<Country> country = service.searchfind(start,count,region);
      PageInfo info = new PageInfo(country);
      mv.addObject("region",region);
      mv.addObject("page", info);
      mv.setViewName("/WEB-INF/view/pagefind.jsp");
    }else{
      mv.setViewName("/findAll/0/10.action");
    }
    return mv;
  }

  @RequestMapping("/seardelete/{code}")
  public View seardelete(@PathVariable("code") String code) {
    int d = service.delete(code);
    System.out.println("成功删除" + d + "条数据");
    View mv = new InternalResourceView("/findAll/0/10.action");
    return mv;
  }

  @RequestMapping("/goadd")
  public View goadd(){
    return new InternalResourceView("/WEB-INF/view/pageadd.jsp");
  }

  @RequestMapping("/add")
  public View add(Country country) {
    service.add(country);
    View view = new InternalResourceView("/findAll/0/10.action");
    return view;

  }

  @RequestMapping("/singlefind/{code}")
  public ModelAndView change(@PathVariable("code") String code) {
    ModelAndView mv = new ModelAndView();
    Country country = service.findbycode(code);
    mv.addObject("country", country);
    mv.setViewName("/WEB-INF/view/pagechange.jsp");
    return mv;
  }

  @RequestMapping("/update")
  public View modify(Country country) {
    int i = service.update(country);
    System.out.println(i);
    View view = new InternalResourceView("/findAll/0/10.action");
    return view;
  }

  @RequestMapping("/delete/{code}")
  public View delete(@PathVariable("code") String code) {
    int d = service.delete(code);
    System.out.println("成功删除" + d + "条数据");
    View mv = new InternalResourceView("/findAll/0/10.action");
    return mv;
  }
}
